//
//  StressViewController.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/3.
//

import UIKit
import MYModule
import EachNavigationBar

class StressDetailViewController: BaseTableViewController {
    
    var provider: StressProvider? {
        
        didSet {
            tableView.reloadData()
        }
        
    }
    
    /// 是否显示right item
    public var showRightItem: Bool = true
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        makeNavigationbar()
        
    }
    
    private lazy var rightBarButtonItem: UIBarButtonItem = {
        let rightBarButtonItem = UIBarButtonItem()
        rightBarButtonItem.image = UIImage(named: "ic_data")
        return rightBarButtonItem
    }()
    
    func makeNavigationbar() {
        
        navigation.item.title = "stress_title".language
        
        if showRightItem {
            navigation.item.rightBarButtonItem = rightBarButtonItem
        }
                                   
        navigation.bar.backgroundColor = UIColor.background
        
        
    }
    
    @objc func historyStressRecord() {
        
        let vc = HistoryController()
        vc.showType = .stress
        self.navigationController?.pushViewController(vc, animated: true)
        
    }
    
    override func makeUI() {
        super.makeUI()
        
        tableView.backgroundColor =  UIColor.darkBackground
        tableView.register(StressStateCell.self, forCellReuseIdentifier: NSStringFromClass(StressStateCell.self))
        tableView.register(StressSevenRecordsCell.self, forCellReuseIdentifier: NSStringFromClass(StressSevenRecordsCell.self))
        
        tableView.dataSource = self
        
        view.addSubview(tableView)
        
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    override func bindViewModel() {
        super.bindViewModel()
        
        rightBarButtonItem.rx.tap.subscribe { [weak self] _ in
            guard let self = self else {return}
            let vc = HistoryController()
            vc.showType = .stress
            self.navigationController?.pushViewController(vc, animated: true)
        }.disposed(by: rx.disposeBag)
    }
    

}

extension StressDetailViewController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(StressStateCell.self)) as! StressStateCell
            
            cell.stateView.stress = provider?.detail?.stress
            cell.stressLabel.text = provider?.detail?.stressTitle
            cell.dateLabel.text = provider?.detail?.date
            
            
            return cell
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(StressSevenRecordsCell.self)) as! StressSevenRecordsCell
        
        cell.stressModels = provider?.detail?.sevenTimesStress
        
        return cell
    }
    
    
    
    
    
    
}
